package cheqex.update;

import javax.swing.*;
import java.awt.GridLayout;
import java.awt.event.*;

import cheqex.update.controllers.AccountHandler;

public class ChangePasswordView extends JPanel implements ActionListener{
	private JLabel newPassword;
	private JLabel confirmNewPassword;
	
	JPasswordField newPasswordField;
	JPasswordField confirmNewPasswordField;
	
	JButton changePassword;
	JPanel changePasswordPanel;
	
	public ChangePasswordView(){
		initialize();
	}
	
	/*	initialize
	 * 	----------
	 * 	AUTHOR: Patrisha Louisse O Sales
	 * 	initialize initializes the components of the 
	 * 	changePasswordView		*/
	public void initialize(){
		this.changePasswordPanel = new JPanel();
		
		this.newPassword =  new JLabel("New Password:");		
		this.confirmNewPassword = new JLabel("Confirm Password:");
		
		this.newPasswordField = new JPasswordField(20);
		newPasswordField.addKeyListener(new KeyListener(){

			@Override
			public void keyPressed(KeyEvent e) {
				if(e.getKeyCode()==KeyEvent.VK_ENTER){
					requestChangePassword();
				}
				
			}

			@Override
			public void keyReleased(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void keyTyped(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}
			
		});

		
		this.confirmNewPasswordField = new JPasswordField(20);
		confirmNewPasswordField.addKeyListener(new KeyListener(){

			@Override
			public void keyPressed(KeyEvent e) {
				if(e.getKeyCode()==KeyEvent.VK_ENTER){
					requestChangePassword();
				}
				
			}

			@Override
			public void keyReleased(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void keyTyped(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}
			
		});
		
		this.changePassword = new JButton("Change Password");
		
		changePassword.setMnemonic(KeyEvent.VK_D);
	    changePassword.setActionCommand("changePassword");
	    changePassword.setEnabled(true);
	    changePassword.addActionListener(this);
	    
	    changePasswordPanel.setLayout(new GridLayout(5,1));
	    changePasswordPanel.add(newPassword);
	    changePasswordPanel.add(newPasswordField);
	    changePasswordPanel.add(confirmNewPassword);
	    changePasswordPanel.add(confirmNewPasswordField);
	    changePasswordPanel.add(changePassword);
	    
	    this.add(changePasswordPanel);
	    this.setVisible(true);
	    
	    
	}
	/*		END OF initialize		*/
	
	public void actionPerformed(ActionEvent e) {
		if ("changePassword".equals(e.getActionCommand())) {
			requestChangePassword();
		}
	}
	
	/*	requestChangePassword
	 * 	---------------------
	 * 	AUTHOR: Patrisha Louisse O Sales
	 * 	requestChangePassword gets the password and the confirmation
	 * 	entered by the user and passes on to the controller
	 * 	to verify if the password entered is a valid password.*/
	private void requestChangePassword(){
		char[] NewPassword = newPasswordField.getPassword();
		char[] ConfirmNewPassword = confirmNewPasswordField.getPassword();
		AccountHandler.VerifyPassword(NewPassword,ConfirmNewPassword);
		newPasswordField.setText(null);
		confirmNewPasswordField.setText(null);
	}
	/*		END OF requestChangePassword*/
}
